package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.itheima.domain.Users;
import com.itheima.mapper.UserMapper;
import com.itheima.service.UserService;
import com.itheima.utils.MD5Util;
import com.itheima.utils.MapperUtil;
import com.itheima.utils.TransactionUtil;
import com.itheima.utils.Uuid;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;

public class UserServiceImpl implements UserService {
    @Override
    public Users login(String username, String password) {
        SqlSession session = null;
        Users user=null;

        session = MapperUtil.getSession();

        UserMapper mapper = session.getMapper(UserMapper.class);

//        password = MD5Util.md5(password);

        user = mapper.findByUsernameAndPassword(username, password);

        session.commit();

        MapperUtil.close(session);

        return user;
    }

    @Override
    public List<Users> findAll(String pageNum, String pageSize) {
        SqlSession session = MapperUtil.getSession();

        UserMapper mapper = session.getMapper(UserMapper.class);

        int pageN = 1;
        int pageS = 10;
        if (StringUtils.isNotBlank(pageNum)){
            pageN = Integer.parseInt(pageNum);
        }
        if (StringUtils.isNotBlank(pageSize)){
            pageS = Integer.parseInt(pageSize);
        }
        PageHelper.startPage(pageN,pageS);

        List<Users> list = mapper.findAll();

        MapperUtil.close(session);

        return list;
    }

    @Override
    public void changePassword(Users users) {
        SqlSession session = MapperUtil.getSession();

        UserMapper mapper = session.getMapper(UserMapper.class);


        mapper.changePassword(users);


        TransactionUtil.commit(session);

        TransactionUtil.close(session);
    }
}
